<template>
  <view class="cart-box" v-if="cart.length !== 0">
    <view class="cart-container">
      <!-- 收获地址组件 -->
      <stop-address></stop-address>
      <!-- 商品列表的标题区域 -->
      <view class="cart-title">
        <!-- 左侧的图标 -->
        <uni-icons type="shop" size="22" style="font-weight: bold;"></uni-icons>
        <!-- 右侧的文本 -->
        <text class="cart-title-text">购物车</text>
      </view>

      <!-- 循环渲染购物车中的商品信息 -->
      <uni-swipe-action>
        <block v-for="(goods, i) in cart" :key="i">
          <uni-swipe-action-item :right-options="options" @click="swipeActionClickHandler(goods)">
            <stop-goods :goods="goods" :show-radio="true" :show-num="true" @radio-change="radioChangeHandler"
              @num-change="numberChangeHandler"></stop-goods>
          </uni-swipe-action-item>
        </block>
      </uni-swipe-action>

      <!-- 使用自定义的结算组件 -->
      <stop-settle></stop-settle>
    </view>

  </view>
  <!-- 空白购物车的区域 -->
  <view class="empty-cart" v-else>
    <text class="tip-text">空空如也~</text>
  </view>
</template>

<script>
  import {
    mapState,
    mapMutations
  } from 'vuex'
  export default {
    computed: {
      ...mapState('m_cart', ['cart']),
    },
    data() {
      return {
        options: [{
          text: '删除',
          style: {
            backgroundColor: 'orangered'
          },
        }]
      };
    },
    methods: {
      ...mapMutations('m_cart', ['updateUserCartList']),
      // 购物车商品状态发生变化
      async radioChangeHandler(e) {
        // 准备参数
        var token = uni.getStorageSync('token')
        const goods = e
        const query = {
          token,
          goods,
        }
        const {
          data: res
        } = await uni.$http.post('/api/update/cart/goodsState', query)
        if (res.meta.status !== 200) return uni.$showMsg('更新购物车商品状态失败')
        this.updateUserCartList(res.message)
      },
      // 购物车商品数量发生变化
      async numberChangeHandler(e) {
        // 准备参数
        var token = uni.getStorageSync('token')
        const goods = e
        const query = {
          token,
          goods,
        }
        const {
          data: res
        } = await uni.$http.post('/api/update/cart/goodsCount', query)
        if (res.meta.status !== 200) return uni.$showMsg('更新购物车商品数量失败')
        this.updateUserCartList(res.message)
      },
      // 点击了滑动删除操作按钮
      async swipeActionClickHandler(goods) {
        // 准备参数
        var token = uni.getStorageSync('token')
        const query = {
          token,
          goods,
        }
        const {
          data: res
        } = await uni.$http.post('/api/delete/cart/goods', query)
        if (res.meta.status !== 200) return uni.$showMsg('删除购物车商品失败')
        this.updateUserCartList(res.message)
      },
    }
  }
</script>

<style lang="scss">
  .cart-box {
    padding: 6px;
    padding-bottom: 50px;
    // border: 1px red solid;
    // height: 100%;
  }
  .cart-container {
    // padding-bottom: 50px;
    // background-color: white;
    // border-radius: 15px;
  }

  .cart-title {
    height: 40px;
    display: flex;
    align-items: center;
    padding-left: 5px;
    // border-bottom: 1px solid #EFEFEF;
    // background-color: white;

    .cart-title-text {
      font-size: 14px;
      margin-left: 4px;
      margin-bottom: 2px;
      font-weight: bold;
    }
  }

  // 空白购物车的区域
  .empty-cart {
    position: relative;
    height: 100vh;

    .tip-text {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      font-size: 14px;
      color: gray;
    }
  }
</style>